<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Kategoriler extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    public function __construct()
    {
        parent::__construct();
            // Your own constructor code
        $this->load->library('MyCategories');   
         if($this->session->userdata('logged_in'))
         {
                $data['sepetim'] = $this->sepetim_model->fill_cart_by_customer_id($this->session->userdata('user_id'));
                $sepet_session = array(
                    'items' => $data['sepetim']['items'],
                    'products' => $data['sepetim']['products'],
                    'total'     => $data['sepetim']['total'],
                    'cart_id'   => $data['sepetim']['cart_id']
                );
                $this->session->set_userdata($sepet_session);
              
         }
         else
         {
            
    
                $data['sepetim'] = $this->sepetim_model->fill_cart_by_session_id( $this->session->userdata['session_id']);
                
                $sepet_session = array(
                    'items' => $data['sepetim']['items'],
                    'products' => $data['sepetim']['products'],
                    'total'     => $data['sepetim']['total'],
                    'cart_id'   => $data['sepetim']['cart_id']
                );
          $this->session->set_userdata($sepet_session);

         }
    } 
    
	public function index()
	{

        $data['categories'] = $this->config_model->get_categories()->result();
        $data['subview'] = 'view_categories2';
        $this->load->view('layouts/default2',$data);
        
	}
    public function urun($product_name,$product_id)
    {
        redirect('urun/'.$product_name.'/'.$product_id);
    }
    public function show_products($category_name = NULL, $subcategory_name = NULL, $brand_name = NULL, $satis_yap = FALSE)
    {
        
        if($category_name != NULL AND $subcategory_name == NULL AND $brand_name == NULL)
        {
            $category_id = $this->mycategories->get_category_name($this->config_model->get_categories()->result(),$category_name);
        
            $subcategories = $this->config_model->get_subcategories($category_id);
            if($subcategories->num_rows()> 0)
            {
                $data['category_id'] = $category_id;
                $data['category_name'] = $this->config_model->get_category_name($category_id)->row(0)->name;
               
                $data['subcategories'] = $this->config_model->get_subcategories($category_id)->result();
                
                $data['subview'] = 'view_subcategories2';
                $this->load->view('layouts/default2',$data);
               
                
            }
            else
            {
                echo "line 57";
            }
        }
        else if($category_name != NULL AND $subcategory_name != NULL AND $brand_name == NULL)
        {
            $category_id = $this->mycategories->get_category_name($this->config_model->get_categories()->result(),$category_name);
            $subcategory_id = $this->mycategories->get_subcategory_name($this->config_model->get_subcategories($category_id)->result(),$subcategory_name);
            
            $subcategories = $this->config_model->get_subcategories($category_id);
            
            if($subcategories->num_rows()> 0)
            {
                $data['category_id'] = $category_id;
                $data['category_name'] = $this->config_model->get_category_name($category_id)->row(0)->name;
                
                $data['subcategory_id'] = $subcategory_id;
                $data['subcategory_name'] = $this->config_model->get_subcategory_name($subcategory_id)->row(0)->name;
                
                //$brand_ids = $this->config_model->get_brands_by_category_and_subcategory($category_id,$subcategory_id);
                if($this->config_model->get_brands_by_category_and_subcategory($category_id,$subcategory_id)->num_rows()>0)
                {
                        $brand_ids = $this->config_model->get_brands_by_category_and_subcategory($category_id,$subcategory_id);
                        foreach($brand_ids->result() as $brand_id)
                        {
                            
                            $brand['brand_id'] = $brand_id->brand_id;
                            $brand['name'] = $this->config_model->get_brand_name($brand_id->brand_id)->row(0)->name;
                            $brandss[] = $brand;
                        }
                        $data['brands'] = $brandss;
                        $data['subview'] = 'view_brands2';
                        $this->load->view('layouts/default2',$data);
                        
                    
                }
                else
                {
                    echo "libne 93";
                }
            }
            else
            {
                echo "line 99 yok";
               
            }            
            
        }
        else if($category_name != NULL AND $subcategory_name != NULL AND $brand_name != NULL)
        {
                $category_id =$category_name; //$this->mycategories->get_category_name($this->config_model->get_categories()->result(),$category_name);
                $subcategory_id =$subcategory_name; //$this->mycategories->get_subcategory_name($this->config_model->get_subcategories($category_id)->result(),$subcategory_name);
                $brand_id = $brand_name; //$this->mycategories->get_brand_name($this->config_model->get_brands()->result(),$brand_name);
              
                $data['category_id'] = $category_id;
                $data['category_name'] = $this->config_model->get_category_name($category_id)->row(0)->name;
                
                $data['subcategory_id'] = $subcategory_id;
                $data['subcategory_name'] = $this->config_model->get_subcategory_name($subcategory_id)->row(0)->name;
                
                $data['brand_id'] = $brand_id;
                $data['brand_name'] = $this->config_model->get_brand_name($brand_id)->row(0)->name;
                
                $pag_url = base_url().'kategoriler/'.$category_name."/".$subcategory_name."/".$brand_name;
                $per_page = $this->_pagination($category_id,$subcategory_id,$brand_id,$pag_url);
               // $data['list_products'] = $this->_capture_products($category_id,$subcategory_id,$brand_id, $per_page,$this->uri->segment(6));
                
                
                $this->db->limit($per_page, $this->uri->segment(5));
                $data['products'] =     $this->db->where(array('brand_id' => $brand_id , 'category_id' => $category_id , 'subcategory_id' => $subcategory_id ))->get('products')->result();
 
              
                    $data['subview'] = 'view_list_products2';
                   $this->load->view('layouts/default2',$data);  
                

                  
        }
        else
        {
            $data['categories'] = $this->config_model->get_categories()->result();
            $data['subview'] = 'view_categories_listbox';
            $this->load->view('layouts/default2',$data);
        }
    }
    public function _pagination($category_id,$subcategory_id,$brand_id,$page_url)
    {
  		
		$config['base_url'] = $page_url;
	
        $config['total_rows'] = $this->db->where(array('brand_id' => $brand_id , 'category_id' => $category_id , 'subcategory_id' => $subcategory_id ))->get('products')->num_rows();
		$config['per_page'] = 10;
		$config['num_links'] = 2;
        $config['uri_segment'] = '5';
        //$config['page_query_string'] = TRUE;
        $config['use_page_numbers'] = TRUE;
        $config['next_link'] = 'Sonraki';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['prev_link'] = 'Önceki';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';        
        
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        

        
		$config['full_tag_open'] = ' <div class="pagination pull-right"><ul>';
		$config['full_tag_close'] = '</ul></div>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
	
		$this->pagination->initialize($config);
        return $config['per_page'];
		
        
		
    }    
    
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */